package com.telecom.cms.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.io.*;
import java.sql.*;

import javax.sql.*;
import com.telecom.cms.dao.GetConnect;

public class DaoImpStd implements Dao {

	private Connection conn = null;
	private Statement stmt = null;
	
	public DaoImpStd(Connection conn1)
	{
		try {
			conn = conn1;
			stmt = conn.createStatement();
		} catch (SQLException ex) {
			System.out.println("Error:insert() Can't get connection!");
		}
	}
	
	public DaoImpStd()
	{
		try {
			conn = (new GetConnect()).getStdConnection();
			stmt = conn.createStatement();
		} catch (SQLException ex) {
			System.out.println("Error:insert() Can't get connection!");
		}
	}
	
	public int insert(String sql) throws Exception {
		int result = 0;
		
		try {
			conn.setAutoCommit(false);
			com.telecom.cms.util.Constant.println(sql);
			result = stmt.executeUpdate(sql);
			conn.commit();
			//stmt.close();
			conn.setAutoCommit(true);
			return result;

		} catch (Exception e) {
			
			conn.rollback();
			conn.setAutoCommit(true);
			System.out.println("Error:Data Insert Failure!"+sql);
			e.printStackTrace();
			throw e;

		} 
	}

	public int delete(String sql) throws Exception {
		int result = 0;
		try {
			conn.setAutoCommit(false);
			//System.out.println(sql);
			result = stmt.executeUpdate(sql);
			conn.commit();
			//stmt.close();
			conn.setAutoCommit(true);
			return result;

		} catch (Exception e) {

			conn.rollback();
			conn.setAutoCommit(true);
			System.out.println("Error:DAO Delete Failure!"+sql);
			throw e;
		} 
	}

	public ResultSet select(String sql) throws Exception {
		ResultSet rs = null;
		try {
			com.telecom.cms.util.Constant.println(sql);

			//FileWriter fw=new FileWriter("c:\\WriteData.txt",true);//建立FileWriter对象，并实例化fw
			//fw.write(sql+"\n");
			//fw.close();
			rs = stmt.executeQuery(sql);
			return rs;
		} catch (Exception e) {
			//stmt.close();
			System.out.println("Error:Select Dao Failure!\n"+sql);
			throw e;
		} 
	}

	public int update(String sql) throws Exception {
		int result = 0;
		try {
			conn.setAutoCommit(false);
			//System.out.println(sql);
			result = stmt.executeUpdate(sql);
			conn.commit();
			//stmt.close();
			conn.setAutoCommit(true);
			return result;
		} catch (Exception e) {

			conn.rollback();
			conn.setAutoCommit(true);
			System.out.println("Error:DAO Update Failure!"+sql);
			throw e;
		} 
	}
	
	public void closeconn()
	{
		/*if(stmt!=null)
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
	}
	
	public boolean checksql(String sql)
	{
		PreparedStatement  stmt=null;
		try {
			System.out.println(sql);
			stmt = conn.prepareStatement(sql);
			
			return true;
		} catch (Exception e) {
			return false;
		} 
		finally
		{
			if(stmt!=null)
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}
